package com.youlai.boot.modules.area.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.area.model.Area;
import com.youlai.boot.modules.area.model.AreaForm;
import com.youlai.boot.modules.area.service.AreaService;
import com.youlai.boot.modules.area.model.AreaPageQuery;
import com.youlai.boot.modules.area.model.AreaVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "15. 区域接口", description = "区域管理接口")
@RestController
@RequestMapping("/api/v1/area")
@RequiredArgsConstructor
public class AreaController {

    private final AreaService areaService;

    @Operation(summary = "区域列表")
    @GetMapping("/page")
    public PageResult<AreaVO> listAreas(AreaPageQuery query) {
        IPage<AreaVO> page = areaService.getAreaPage(query);
        return PageResult.success(page);
    }

    @Operation(summary = "区域详情")
    @GetMapping("/{id}")
    public Result<Area> getArea(@PathVariable Long id) {
        Area area = areaService.getById(id);
        return Result.success(area);
    }

    @Operation(summary = "获取表单")
    @GetMapping("/{id}/form")
    public Result<AreaForm> getAreaForm(@PathVariable Long id) {
        AreaForm form = areaService.getAreaForm(id);
        return Result.success(form);
    }

    @Operation(summary = "新增区域")
    @PostMapping
    public Result<Boolean> addArea(@RequestBody Area area) {
        boolean result = areaService.save(area);
        return Result.judge(result);
    }

    @Operation(summary = "修改区域")
    @PutMapping("/{id}")
    public Result<Boolean> updateArea(@PathVariable Long id, @RequestBody Area area) {
        area.setId(id);
        boolean result = areaService.updateById(area);
        return Result.judge(result);
    }

    @Operation(summary = "删除区域")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteArea(@PathVariable Long id) {
        boolean result = areaService.removeById(id);
        return Result.judge(result);
    }
}